'use strict';

/* eslint-disable */
function encodeReg(source) {
  return String(source).replace(/([.*+?^=!:${}()|[\]/\\])/g, "\\$1");
}
function getInnerString(source, prefix, postfix) {
  var regexp = new RegExp(
    encodeReg(prefix) + "(?:(?!</p>)[\\s\\S])*" + encodeReg(postfix),
    "gi"
  );
  var matches = String(source).match(regexp);
  if (matches == null) {
    return null;
  }
  var formatedMatches = matches.map((value) => {
    return value.replace(prefix, "").replace(postfix, "");
  });
  return formatedMatches;
}

var path = window.location.pathname;
if (path.indexOf("_webprj") !== -1) {
  if (path.indexOf(".jsp") !== -1) {
    var result = getInnerString(window.location.pathname, "_webprj", ".jsp");
    path = result ? result[0] : '/';
  } else {
    path = '/';
  }
}
var ulElem = document.getElementById('menu');
console.log('path:', path);
switch (path) {
  case '/index':
  case '/': {
    highlight(ulElem, 0);
    break;
  }
  case '/events': {
    highlightSiderBar('allEvents');
    highlight(ulElem, 1);
    break;
  }
  case '/news': {
    highlightSiderBar('allNews');
    highlight(ulElem, 1);
    break;
  }
  case '/research_detail':
  case '/research': {
    highlight(ulElem, 2);
    break;
  }
  case '/patents':
  case '/books':
  case '/publication': {
    highlight(ulElem, 3);
    break;
  }
  case '/alumni':
  case '/ms':
  case '/phd':
  case '/fellows':
  case '/faculty_staff':
  case '/tan':
  case '/members': {
    highlight(ulElem, 4);
    break;
  }
  case '/contact': {
    highlight(ulElem, 5);
    break;
  }
  default: break;
}
function highlight(ulElem, index) {
  if (!ulElem) {
    return;
  }
  for (let i = 0; i < ulElem.children.length; i++) {
    ulElem.children[i].className = i === index ? 'active' : '';
  }
}
function highlightSiderBar(id) {
  var elem = document.getElementById(id);
  if (id.startsWith('all')) {
    // 设置当前按钮高亮
    elem.classList.add('my-btn__active');
    if (id === 'allNews') {
      document.getElementById('allEvents').className = 'my-btn sidebar-item pointer';
    } else {
      document.getElementById('allNews').className = 'my-btn sidebar-item pointer';
    }
  } else {
    // 书籍、期刊

  }
}
